package cn.slt.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableColumnModelListener;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.junit.Test;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.slt.domian.MD_ClassDSID;
import cn.slt.domian.MD_Metadata;
import cn.slt.domian.MD_ObjDSID;
import cn.slt.domian.MD_libraryDSID;
import cn.slt.service.MD_MetadataService;
import cn.slt.service.MD_ObjDSIDService;
import cn.slt.service.MD_classDSIDService;
import cn.slt.service.MD_libraryDSIDService;

@Controller
public class ExportDictionaryController {
	@Autowired
	private MD_MetadataService MD_metadataService;
	@Autowired
	private MD_libraryDSIDService MD_libraryDSIDService;
	@Autowired
	private MD_classDSIDService MD_classDSIDService;
	@Autowired
	private MD_ObjDSIDService MD_objDSIDService;

	@RequestMapping("printWord")
	@ResponseBody
	public void createWord2007() {
		XWPFDocument doc = new XWPFDocument();
		List<MD_Metadata> metadatas = new ArrayList<MD_Metadata>();
		List<MD_libraryDSID> libraryDSIDs=new ArrayList<MD_libraryDSID>();
		List<MD_ClassDSID>classDSIDs=new ArrayList<MD_ClassDSID>();
		List<MD_ObjDSID>objDSIDs=new ArrayList<MD_ObjDSID>();
		metadatas = MD_metadataService.getEveryone();
		classDSIDs=MD_classDSIDService.getEveryone();
		createMetaTable(doc, metadatas, metadatas.size(), 1);
		createClassTable(doc, classDSIDs, classDSIDs.size(),1);
		// XWPFParagraph p1 = doc.createParagraph();
		// XWPFTable table = doc.createTable(11, 4);
		// // CTTblBorders
		// borders=table.getCTTbl().getTblPr().addNewTblBorders();
		// CTTblPr tblPr = table.getCTTbl().getTblPr();
		// tblPr.getTblW().setType(STTblWidth.DXA);
		// tblPr.getTblW().setW(new BigInteger("8000"));
		// // 设置上下左右四个方向的距离，可以将表格撑大
		// table.setCellMargins(20, 20, 20, 20);
		//
		// // 表格
		// List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
		//
		// XWPFTableCell cell = tableCells.get(0);
		// XWPFParagraph newPara = new XWPFParagraph(cell.getCTTc().addNewP(),
		// cell);
		// XWPFRun run = newPara.createRun();
		// /** 内容居中显示 **/
		// newPara.setAlignment(ParagraphAlignment.CENTER);
		// //
		// run.getCTR().addNewRPr().addNewColor().setVal("FF0000");/**FF0000红色*/
		// // run.setUnderline(UnderlinePatterns.THICK);
		// run.setText("1");
		// run.setText("11");
		// run.setText("111");
		//
		// tableCells.get(1).setText("2");
		// tableCells.get(2).setText("3");
		// tableCells.get(3).setText("4");
		//
		// tableCells = table.getRow(1).getTableCells();
		// tableCells.get(0).setText("5");
		// tableCells.get(1).setText("6");
		// tableCells.get(2).setText("7");
		// tableCells.get(3).setText("8");

		// XWPFParagraph p5 = doc.createParagraph();
		//
		// XWPFTable table5 = doc.createTable(11, 4);
		// tableCells = table5.getRow(2).getTableCells();
		//
		// tableCells.get(0).setText("5");
		// tableCells.get(1).setText("6");
		// tableCells.get(2).setText("7");
		// tableCells.get(3).setText("8");
		//
		// XWPFParagraph p6 = doc.createParagraph();
		//
		// XWPFTable table6 = doc.createTable(11, 4);
		// tableCells = table6.getRow(2).getTableCells();
		//
		// tableCells.get(0).setText("5");
		// tableCells.get(1).setText("6");
		// tableCells.get(2).setText("7");
		// tableCells.get(3).setText("8");
		//
		// tableCells = table.getRow(7).getTableCells();
		// tableCells.get(0).setText("5");
		// tableCells.get(1).setText("6");
		// tableCells.get(2).setText("7");
		// tableCells.get(3).setText("8");
		//
		// tableCells = table.getRow(8).getTableCells();
		// tableCells.get(0).setText("5");
		// tableCells.get(1).setText("6");
		// tableCells.get(2).setText("7");
		// tableCells.get(3).setText("8");
		//

		// // 设置字体对齐方式
		// p1.setAlignment(ParagraphAlignment.CENTER);
		// p1.setVerticalAlignment(TextAlignment.TOP);
		//
		//
		// // 第一页要使用p1所定义的属性
		// XWPFRun r1 = p1.createRun();
		//
		// // 设置字体是否加粗
		// r1.setBold(true);
		// r1.setFontSize(20);
		//
		// // 设置使用何种字体
		// r1.setFontFamily("宋体");
		//
		// // 设置上下两行之间的间距
		// r1.setTextPosition(20);
		// r1.setText("标题");

		FileOutputStream out;
		try {
			out = new FileOutputStream("e:/数据目录.docx");
			// 以下代码可进行文件下载
			// response.reset();
			// response.setContentType("application/x-msdownloadoctet-stream;charset=utf-8");
			// response.setHeader("Content-Disposition",
			// "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8"));
			// OutputStream out = response.getOutputStream();
			// this.doc.write(out);
			// out.flush();

			doc.write(out);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("success");
	}

	public void createMetaTable(XWPFDocument doc, List<MD_Metadata> metadatas, int rows, int cols) {
		XWPFParagraph p = doc.createParagraph();
		int i = 1;
		p.setAlignment(ParagraphAlignment.CENTER);
		XWPFTable table = doc.createTable(rows + 1, 9);
		CTTblPr tblPr = table.getCTTbl().getTblPr();
		tblPr.getTblW().setType(STTblWidth.DXA);
		tblPr.getTblW().setW(new BigInteger("8000"));
		tblPr.addNewJc().setVal(STJc.CENTER);

		// 设置上下左右四个方向的距离，可以将表格撑大
		table.setCellMargins(20, 20, 20, 20);
		List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
		tableCells.get(0).setText("元数据编码");
		tableCells.get(1).setText("元数据名称");
		tableCells.get(2).setText("地区编码");
		tableCells.get(3).setText("元数据创建单位");
		tableCells.get(4).setText("元数据创建人");
		tableCells.get(5).setText("创建日期");
		tableCells.get(6).setText("元数据标准");
		tableCells.get(7).setText("绑定数据库owner");
		tableCells.get(8).setText("绑定数据库URL");

		for (MD_Metadata metadata : metadatas) {
			
			// p = doc.createParagraph();
			// List<XWPFTableRow> xrows =table.getRows();
			// tableCells=xrows.get(i).getTableCells();
			tableCells = table.getRow(i).getTableCells();
			i++;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(metadata.getBuildDate());
			tableCells.get(0).setText(metadata.getAreaCod());
			tableCells.get(1).setText(metadata.getMdName());
			tableCells.get(2).setText(metadata.getAreaCod());
			tableCells.get(3).setText(metadata.getBuildUnit());
			tableCells.get(4).setText(metadata.getStaff());
			tableCells.get(5).setText(str);
			tableCells.get(6).setText(metadata.getStandName());
			tableCells.get(7).setText(metadata.getOwner());
			tableCells.get(8).setText(metadata.getLocation());

		}
		XWPFRun r1 = p.createRun();

		// 设置字体是否加粗
		r1.setBold(true);
		r1.setFontSize(18);

		// 设置使用何种字体
		r1.setFontFamily("宋体");

		// 设置上下两行之间的间距
		r1.setTextPosition(20);
		r1.setText("元数据列表");

	}
	public void createLibTable(XWPFDocument doc, List<MD_libraryDSID> libraryDSIDs, int rows, int cols) {
		XWPFParagraph p = doc.createParagraph();
		int i = 1;
		p.setAlignment(ParagraphAlignment.CENTER);
		XWPFTable table = doc.createTable(rows + 1, 9);
		CTTblPr tblPr = table.getCTTbl().getTblPr();
		tblPr.getTblW().setType(STTblWidth.DXA);
		tblPr.getTblW().setW(new BigInteger("8000"));
		tblPr.addNewJc().setVal(STJc.CENTER);

		// 设置上下左右四个方向的距离，可以将表格撑大
		table.setCellMargins(20, 20, 20, 20);
		List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
		tableCells.get(0).setText("元数据编码");
		tableCells.get(1).setText("元数据名称");
		tableCells.get(2).setText("地区编码");
		tableCells.get(3).setText("元数据创建单位");
		tableCells.get(4).setText("元数据创建人");
		tableCells.get(5).setText("创建日期");
		tableCells.get(6).setText("元数据标准");
		tableCells.get(7).setText("绑定数据库owner");
		tableCells.get(8).setText("绑定数据库URL");

		for (MD_Metadata metadata : metadatas) {
			if(metadata.getMdName().equals("")) continue;
			// p = doc.createParagraph();
			// List<XWPFTableRow> xrows =table.getRows();
			// tableCells=xrows.get(i).getTableCells();
			tableCells = table.getRow(i).getTableCells();
			i++;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(metadata.getBuildDate());
			tableCells.get(0).setText(metadata.getAreaCod());
			tableCells.get(1).setText(metadata.getMdName());
			tableCells.get(2).setText(metadata.getAreaCod());
			tableCells.get(3).setText(metadata.getBuildUnit());
			tableCells.get(4).setText(metadata.getStaff());
			tableCells.get(5).setText(str);
			tableCells.get(6).setText(metadata.getStandName());
			tableCells.get(7).setText(metadata.getOwner());
			tableCells.get(8).setText(metadata.getLocation());

		}
		XWPFRun r1 = p.createRun();

		// 设置字体是否加粗
		r1.setBold(true);
		r1.setFontSize(18);

		// 设置使用何种字体
		r1.setFontFamily("宋体");

		// 设置上下两行之间的间距
		r1.setTextPosition(20);
		r1.setText("元数据列表");

	}
	public void createClassTable(XWPFDocument doc, List<MD_ClassDSID> classDSIDs, int rows, int cols) {
		XWPFParagraph p = doc.createParagraph();
		int i = 1;
		p.setAlignment(ParagraphAlignment.CENTER);
		XWPFTable table = doc.createTable(rows + 1, 5);
		CTTblPr tblPr = table.getCTTbl().getTblPr();
		tblPr.getTblW().setType(STTblWidth.DXA);
		tblPr.getTblW().setW(new BigInteger("8000"));
		tblPr.addNewJc().setVal(STJc.CENTER);

		// 设置上下左右四个方向的距离，可以将表格撑大
		table.setCellMargins(20, 20, 20, 20);
		List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
		tableCells.get(0).setText("表中文名");
		tableCells.get(1).setText("表英文名");
		tableCells.get(2).setText("创建日期");
		tableCells.get(3).setText("关键字");
		tableCells.get(4).setText("所属数据库");
		
		

		for (MD_ClassDSID classDSID : classDSIDs) {
			if(classDSID.getDsName().equals("")) continue;
			// p = doc.createParagraph();
			// List<XWPFTableRow> xrows =table.getRows();
			// tableCells=xrows.get(i).getTableCells();
			tableCells = table.getRow(i).getTableCells();
			i++;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str=null;
			if(classDSID.getBuildDate()!=null)
			 str = sdf.format(classDSID.getBuildDate());
			else  str="";
			tableCells.get(0).setText(classDSID.getBeizhu());
			tableCells.get(1).setText(classDSID.getDsName());
			tableCells.get(2).setText(str);
			tableCells.get(3).setText(classDSID.getKeywords());
			tableCells.get(4).setText(classDSID.getSupDsCode().toString());
			
			

		}
		XWPFRun r1 = p.createRun();

		// 设置字体是否加粗
		r1.setBold(true);
		r1.setFontSize(18);

		// 设置使用何种字体
		r1.setFontFamily("宋体");

		// 设置上下两行之间的间距
		r1.setTextPosition(20);
		r1.setText("表信息");

	}
	public void createObjTable(XWPFDocument doc, List<MD_ObjDSID> objDSIDs, int rows, int cols) {
		XWPFParagraph p = doc.createParagraph();
		int i = 1;
		p.setAlignment(ParagraphAlignment.CENTER);
		XWPFTable table = doc.createTable(rows + 1, 9);
		CTTblPr tblPr = table.getCTTbl().getTblPr();
		tblPr.getTblW().setType(STTblWidth.DXA);
		tblPr.getTblW().setW(new BigInteger("8000"));
		tblPr.addNewJc().setVal(STJc.CENTER);

		// 设置上下左右四个方向的距离，可以将表格撑大
		table.setCellMargins(20, 20, 20, 20);
		List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
		tableCells.get(0).setText("元数据编码");
		tableCells.get(1).setText("元数据名称");
		tableCells.get(2).setText("地区编码");
		tableCells.get(3).setText("元数据创建单位");
		tableCells.get(4).setText("元数据创建人");
		tableCells.get(5).setText("创建日期");
		tableCells.get(6).setText("元数据标准");
		tableCells.get(7).setText("绑定数据库owner");
		tableCells.get(8).setText("绑定数据库URL");

		for (MD_Metadata metadata : metadatas) {
			if(metadata.getMdName().equals("")) continue;
			// p = doc.createParagraph();
			// List<XWPFTableRow> xrows =table.getRows();
			// tableCells=xrows.get(i).getTableCells();
			tableCells = table.getRow(i).getTableCells();
			i++;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String str = sdf.format(metadata.getBuildDate());
			tableCells.get(0).setText(metadata.getAreaCod());
			tableCells.get(1).setText(metadata.getMdName());
			tableCells.get(2).setText(metadata.getAreaCod());
			tableCells.get(3).setText(metadata.getBuildUnit());
			tableCells.get(4).setText(metadata.getStaff());
			tableCells.get(5).setText(str);
			tableCells.get(6).setText(metadata.getStandName());
			tableCells.get(7).setText(metadata.getOwner());
			tableCells.get(8).setText(metadata.getLocation());

		}
		XWPFRun r1 = p.createRun();

		// 设置字体是否加粗
		r1.setBold(true);
		r1.setFontSize(18);

		// 设置使用何种字体
		r1.setFontFamily("宋体");

		// 设置上下两行之间的间距
		r1.setTextPosition(20);
		r1.setText("元数据列表");

	}
}
